<template>
  <!-- 分配权限的弹层 -->

  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeDialog"
    @open="openbtn"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      node-key="id"
      :data="permissionData"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
    />
    <template #footer>
      <div style="text-align: right;">
        <el-button>取消</el-button>
        <el-button type="primary" @click="clickAssign">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/tranListDepartments'
import { assignPerm, getRoleDetailById } from '@/api/setting'
export default {
  props: {
    showAssignDialog: {
      type: Boolean
    },
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      permissionData: []
    }
  },
  methods: {
    async hGetPermissionList() {
      // 发送请求, 获取权限列表
      const res = await getPermissionList()
      console.log(res)
      this.permissionData = tranListToTreeData(res.data)
    },
    async openbtn() {
      this.hGetPermissionList()
      const res = await getRoleDetailById({ id: this.roleId })
      console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    closeDialog() {
      this.$emit('closeDialog')
    },
    async clickAssign() {
      const res = await assignPerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      console.log(res)
      this.$message.success('分配成功')
      this.closeDialog()
    }
  }
}
</script>
